<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item />
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span>操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="adddept" :title="title" :bmlist="bmlist" :dialog="showDialog" :cur-node="curNode" @closeDialog="close" @close="close" />
  </div>
</template>

<script>
import AddDept from '@/views/departments/children/add-dept'
import { getDepartments, delDepartments } from '@/api/departments'
import { transTreeData } from './transTreeData'
export default {
  components: {
    AddDept
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curNode: {},
      bmlist: [],
      title: '新增部门',
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this._getDepartments()
  },
  methods: {
    async _getDepartments() {
      const res = await getDepartments()
      this.bmlist = res.depts
      this.list = transTreeData(res.depts)
    },
    del(data) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delDepartments(data.id)
        // 添加提示
        this.$message.success('删除数据成功')
        // 更新数据
        this._getDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    add(data) {
      this.title = '新增部门'
      this.showDialog = true
      this.curNode = data
    },
    edit(data) {
      this.title = '编辑部门'
      // 打开弹框
      this.showDialog = true
      // 数据
      // this.curNode = data
      this.$refs.adddept.hGetDepartDetail(data.id)
    },
    close() {
      this.showDialog = false
      this._getDepartments()
      // this.
    }
  }
}
</script>

<style scoped lang='scss'>
</style>
